import { _decorator, Component, Node,Label,Tween, tween,Color, color, Vec3, v3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Tip')
export class Tip extends Component {

    public label:Label=null;

    public inited:boolean=false;

    public static instance=null;

    public static readonly defaultColor:Color=color(105, 255, 165, 255);

    onLoad()
    {
        Tip.instance=this;
        this.init();
    }

    public init()
    {
        this.label=this.node.getChildByName("Label").getComponent(Label);
        this.label.node.active=false;
        this.inited=true;
    }

    public showTip(text:string=null)
    {
        if(text)
            Tip.instance.label.string=text;

        console.log("** tip show");

        this.label.node.active=true;
        this.label.node.setScale(0.2, 0.2, 1);
        this.label.color=Tip.defaultColor;

        tween(this.label.node).to(0.45, {scale:Vec3.ONE },{easing:"backOut"}).call(()=>{

        }).start();
        this.scheduleOnce(()=>{
            tween(this.label.node).to(1, { position: v3(0, 120, 0)},{
                onUpdate: (target, ratio: number) => {
                    this.label.color = color(Tip.defaultColor.r, Tip.defaultColor.g, Tip.defaultColor.b, 255 * (1-ratio));
                }
            }).call(()=>{
                this.label.node.active=false;
            }).start();
        }, 1.5);
    }

    public static show(text:string=null)
    {
        if(Tip.instance===null)
            return;
        if(!Tip.instance.inited)
            Tip.instance.init();

        Tip.instance.showTip(text);
    }

}

